#include "Tlc5940.h"
#include "LEDControl.h"
cLED LEDs[10];

void setup() {
  Serial.begin(19200);
  Serial.println("Hello");
  
  //setupColrtest();
  Tlc.init();
  for (int x=0;x<32;x+=3)
  {
    if(x==15)
      {LEDs[x/3].init(x,x+3,x+2); x++;}
    else
      {LEDs[x/3].init(x,x+2,x+1);}
  }
}

void loop() {
  serial();
  heartbeat();
  Tlc.update();
}

uint8_t hbval=128;
int8_t hbdelta=8;
void heartbeat() {
  if (hbval > 192) hbdelta = -hbdelta;
  if (hbval < 32) hbdelta = -hbdelta;
  hbval += hbdelta;
  analogWrite(6, hbval);
  delay(40);
}

uint8_t buff[256]; // global block storage
uint8_t getch() {
  return Serial.read();
}

void readbytes(int n) {
  for (int x = 0; x < n; x++) {
    buff[x] = Serial.read();
  }
}

void serial(){
  byte r;
  uint8_t ch = getch();
  
  switch (ch) {
    
  case '0':
    pinMode(6, OUTPUT);
    digitalWrite(6, HIGH);
    delay(500);
    digitalWrite(6 , LOW);
    delay(500);
    digitalWrite(6, HIGH);
    delay(500);
    digitalWrite(6, LOW);
    delay(500);
    digitalWrite(6, HIGH);
    delay(500);
    digitalWrite(6, LOW);
    delay(500);  
    break;
    
  case '1':
    readbytes(3);
    r=buff[0];
    if(r == 64){
      Serial.println("K");
    }
    else{
      Serial.print("X");
      Serial.println(r, DEC); }
    break;
    
  case '3':
    readbytes(3);
    for(int x=0;x<10;x++)
    {
       //LEDs[x].setColor(buff[0],buff[1],buff[2]);
    }
    
    Serial.print("C");
    delay(10);
    Serial.print(buff[0], DEC);
    delay(10);
    Serial.print(" ");
    delay(10);
    Serial.print(buff[1], DEC);
    delay(10);
    Serial.print(" ");
    delay(10);
    Serial.println(buff[2], DEC);
    delay(10);
    break;
  }
}
